from ironcap.serialization import dictserializer

#=========================================================================
# ironcap.serialization.xmlserializer.dict_to_xml
#=========================================================================

def dict_to_xml(obj):
	ret = ''
	for k, v in obj.iteritems():
		if isinstance(v, dict):
			ret = ''.join([ret, '<', k, '>', dict_to_xml(v), '</', k, '>'])
			
		elif hasattr(v, "__iter__"):
			ret = ''.join([ret, '<' , k, '>'])
			for n in v:
				ret = ''.join([ret, '<i>', str(n), '</i>'])
			ret = ''.join([ret, '</' , k, '>'])
			
		else:
			ret = ''.join([ret, '<', k, '>', str(v), '</', k , '>'])
			
	return ret

#=========================================================================
# ironcap.serialization.xmlserializer.serialize
#=========================================================================
	
def serialize(obj):
	""" serializes this object to a json string """
	if hasattr(obj, '__iter__'):
		ret = ''
		for n in obj:
			d   = dictserializer.serialize(n) 
			ret = ''.join([ret, '<i>', dict_to_xml(d), '</i>'])
		return ret
	else:
		d = dictserializer.serialize(obj) 
		return dict_to_xml(d)